package home_8_21;

import java.util.*;

public class Main {
    public static final List<Map<String,Object>> books = new ArrayList<>();
    public static final  String[] menus = {"查询","修改","添加","删除","退出"};
    public static final  String[] cols = {"no","name","price","num","zuozhe"};
    public static void main(String[] args) {
        System.out.println("");
        System.out.println("***********");
        Scanner scanner = new Scanner(System.in);

        while (true){
            for (int i = 0; i < menus.length; i++) {
                System.out.println(i+1+"."+menus[i]);
            }
            System.out.print("请选择：");
            String n = scanner.nextLine();
            switch (n){
                case "1":
                    chaxun();
                    break;
                case "2":

                    break;
                case "3":
                    tianjia();
                    //添加图书
                    break;
                case "4":
                    shanchu();
                    break;
                case "5":

            }

        }

    }
    private static void chaxun(){
        System.out.println("查询到的信息如下");
        if (books.size()==0){
            System.out.println("图书信息为空");
            return;
        }
        for (int i = 0; i <  cols.length; i++) {
            System.out.print(cols[i]+"\t");
        }
        System.out.println();
        for(Map<String,Object> b:books){
            for (String k:cols){
                Object obj = b.get(k);
                System.out.print(obj+"\t");
            }
            System.out.println();
        }



    }
    private static void shanchu(){
        Scanner in = new Scanner(System.in);
        System.out.println("输入要删除的书编号");
        String no = in.nextLine();
        Map<String,Object> book = null;
        for (Map<String,Object> b:books){
            if(no.equals(b.get("no"))){
                book = b;
                break;

            }
        }
        if (book==null){
            System.out.println("图书编号有误");
            return;
        }
        boolean falg =books.remove(book);
        System.out.println(falg==true? "删除成功":"删除失败");
    }
    private static void tianjia(){
        System.out.println("请输入图书信息");
        Scanner in = new Scanner(System.in);
        Map<String,Object> book = new HashMap<>();
        //构建图书的对象，代替以前的实体类用于存放数据。
        for (int i = 0; i < cols.length; i++) {
            String col = cols[i];
            System.out.println(col+":");
            String str = in.nextLine();
            book.put(col,str);
        }
        books.add(book);
        System.out.println("添加完毕");
    }
}
